package math;

/**
 * Created by fengliejv on 2022/4/5.
 */
public class PrimeNumberOfSetBitsInBinaryRepresentation762 {
    public int countPrimeSetBits(int left, int right) {
        int result = 0;
        for (int i = left; i <= right; i++) {
            int num = calcuOne(i);
            if(isPrime(num)){
                result++;
            }
        }
        return result;
    }

    private boolean isPrime(int num) {
        if(num<2){
            return false;
        }
        if(num==2){
            return true;
        }
        for (int i = 2; i*i <= num ; i++) {
            if(num%i==0){
                return false;
            }
        }
        return true;
    }

    private int calcuOne(int number) {
        int count = 0;
        while (number!=0){
            if((number&1)==1){
                count++;
            }
            number = number>>1;
        }
        return count;
    }
}
